﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ImageUpload.ascx.cs"
  Inherits="NguyenHoa.Web.Controls.ImageUpload" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxHiddenField" TagPrefix="dxhf" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dxe" %>
<%@ Register Assembly="DevExpress.Web.v9.1, Version=9.1.4.0, Culture=neutral, PublicKeyToken=5377c8e3b72b4073"
  Namespace="DevExpress.Web.ASPxUploadControl" TagPrefix="dxuc" %>
<table>
  <tr>
    <td style="border: 1px dashed #808080; float: left; width: 110px; height: 110px;">
      <dxe:ASPxImage ID="imagePreview" ClientInstanceName="imagePreview" runat="server"
        ImageUrl="" Width="110px" Height="110px">
      </dxe:ASPxImage>
    </td>
    <td valign="bottom" style="padding-left: 10px;">
      <div style="float: left;">
        <dxuc:ASPxUploadControl ID="uploader" runat="server" ClientInstanceName="uploader"
          Size="35" OnFileUploadComplete="uploader_FileUploadComplete" FileUploadMode="OnPageLoad">
          <ClientSideEvents FileUploadComplete="function(s, e) { Uploader_OnUploadComplete(e); }"
            FileUploadStart="function(s, e) { Uploader_OnUploadStart(); }" TextChanged="function(s,e){uploadFile(this);}">
          </ClientSideEvents>
          <ValidationSettings AllowedContentTypes="image/jpeg,image/gif,image/png,image/pjpeg"
            MaxFileSize="10485760" FileDoesNotExistErrorText="NOT_EXIST" GeneralErrorText="GENERAL_ERR"
            MaxFileSizeErrorText="MAX_SIZE" NotAllowedContentTypeErrorText="ERROR_TYPE">
            <ErrorStyle Font-Size="0pt" />
          </ValidationSettings>
          <ProgressBarStyle Height="25px">
          </ProgressBarStyle>
        </dxuc:ASPxUploadControl>
      </div>
      <div style="clear: both;">
        <asp:Label ID="lblNotice" runat="server" Text="<%$Resources:Messages, ERR0024 %>"></asp:Label>
      </div>
    </td>
  </tr>
</table>
<div style="clear: both;">
  <asp:HiddenField ID="hfImagePath" runat="server" />
</div>

<script type="text/javascript" language="javascript">
  function Uploader_OnUploadComplete(args) {
    var imgSrc = "../images/ImagePreview.png";
    var files = args.callbackData.split('|');
    if (args.isValid) {
      imgSrc = "../Upload/Temp/" + files[0];
    }
    else {
      if (args.errorText != '') {
        //alert(args.errorText);
        var error = args.errorText;
        switch (args.errorText) {
          case 'NOT_EXIST':
            error = '<%=(string)GetGlobalResourceObject("Messages", "ERR0020") %>';
            break;
          case 'GENERAL_ERR':
            error = '<%=(string)GetGlobalResourceObject("Messages", "ERR0022") %>';
            break;
          case 'MAX_SIZE':
            error = '<%=(string)GetGlobalResourceObject("Messages", "ERR0023") %>';
            break;
          case 'ERROR_TYPE':
            error = '<%=(string)GetGlobalResourceObject("Messages", "ERR0021") %>';
            break;
        }
        
        alertMessage(error);
        return;
      }
      if (args.callbackData.indexOf('ERROR') >= 0) {
        var error = args.callbackData.split('|');
        alertMessage(error[1]);
      }
      else
        return;
    }
    imagePreview.SetImageUrl(imgSrc);
    document.getElementById('<%=hfImagePath.ClientID %>').value = 'Upload/Images/' + files[1];
  }

  function Uploader_OnUploadStart() {
  }

  function uploadFile(file) {
    uploader.Upload();
  }

  function getImageName() {
    return document.getElementById('<%=hfImagePath.ClientID %>').value;
  }

  function setImageName(name) {
    document.getElementById('<%=hfImagePath.ClientID %>').value = name;
    imagePreview.SetImageUrl(name);
  }

  function hideUpload() {
    uploader.SetVisible(false);
    var lblNotice = document.getElementById('<%=lblNotice.ClientID %>');
    lblNotice.style.display = 'none';
  }
</script>

